var jump_time_out = 500;
var getChatContentTime = 1000;

var wang_editor = window.wangEditor;
var article_editor = new wang_editor('#article_editor');
article_editor.create();

var article_list_children_number = $('#article_list').children();

if(article_list_children_number.length < 6)
{
    $('#click_load_more_button').text('没有更多了！快去写一篇吧！');
    setButtonDisabled('#click_load_more_button');
}

setInterval(function(){
    get_chat_content();
}, getChatContentTime);

function get_chat_content()
{
    var send_chat_id = $('input[name="chat_last_id"]').val();
    $.ajax({
        url:'/getChatContent',
        type:"post",
        dataType:"json",
        data:{
            chat_last_id:send_chat_id
        },
        success:function(returnData){
            if (returnData.length > 0)
            {
                var chat_id = parseInt(send_chat_id);
                chat_id++;
                $('input[name="chat_last_id"]').val(chat_id);
                var contentStr = '';
                $.each(returnData, function(index, item){
                    contentStr += '<div class="grid-css grid-css--masonry" data-col-lg="1" data-col-md="1" data-col-sm="1" data-col-xs="1" data-gap="30" style="padding-top: 1em;">';
                    contentStr += '<div class="grid__inner">';
                    contentStr += '<div class="grid-sizer"></div>';
                    contentStr += '<div class="grid-item animated flipInX">';
                    contentStr += '<div class="grid-item__inner">';
                    contentStr += '<div class="grid-item__content-wrapper">';
                    contentStr += '<div class="post">';
                    contentStr += '<div class="post__body" style="padding-top: 10px;padding-bottom: 10px;">';
                    contentStr += '<div class="post__meta">';
                    contentStr += '<span class="date">';
                    contentStr += '<a href="#">';
                    contentStr += item['user_id']['name'];
                    contentStr += '</a>';
                    contentStr += '</span>';
                    contentStr += '<br>';
                    contentStr += '<span class="date">';
                    contentStr += item['create_time'];
                    contentStr += '</span>';
                    contentStr += '</div>';
                    contentStr += '<h2 class="post__title">';
                    contentStr += item['chat_content'];
                    contentStr += '</h2>';
                    contentStr += '<p class="post__text" style="margin-bottom: 0;">';
                    contentStr += item['ip'];
                    contentStr += '</p>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                    contentStr += '</div>';
                });
                $('#chat_list').prepend(contentStr);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#input_chat_room_button').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var chat_content = $('input[name="chat_content"]').val();
        input_chat_room_button('/inputChatRoom', token, chat_content, '#input_chat_room_button')
    }
);

function input_chat_room_button(url, token, chat_content, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            chat_content:chat_content
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('发送中...');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    layer.msg('成功！');
                    $('input[name="chat_content"]').val('');
                    removeButtonDisabled(buttonName);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#click_load_more_button').on('click',function()
    {
        var article_page = $('input[name="article_page"]').val();
        click_load_more_button('/getMoreArticle', article_page, '#click_load_more_button')
    }
);

function click_load_more_button(url, article_page, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            article_page:article_page
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...');
        },
        success:function(returnData){
            var contentStr = '';
            $.each(returnData, function(index, item){
                contentStr += '<div class="grid-css grid-css--masonry" data-col-lg="1" data-col-md="1" data-col-sm="1" data-col-xs="1" data-gap="30" style="padding-top: 1em;">';
                contentStr += '<div class="grid__inner">';
                contentStr += '<div class="grid-sizer"></div>';
                contentStr += '<div class="grid-item animated flipInX">';
                contentStr += '<div class="grid-item__inner">';
                contentStr += '<div class="grid-item__content-wrapper">';
                contentStr += '<div class="post">';
                contentStr += '<div class="post__body">';
                contentStr += '<div class="post__meta">';
                contentStr += '<span class="date">';
                contentStr += item['create_time'];
                contentStr += '</span>';
                contentStr += '</div>';
                contentStr += '<h2 class="post__title">';
                contentStr += '<a href='+'/'+$.base64.encode(item['id'])+'>';
                contentStr += item['article_title'];
                contentStr += '</a>';
                contentStr += '</h2>';
                contentStr += '<a class="md-btn md-btn--outline-primary" href='+'/'+$.base64.encode(item['id'])+'>';
                contentStr += '点击回忆';
                contentStr += '</a>';
                contentStr += '</div>';
                contentStr += '</div>';
                contentStr += '</div>';
                contentStr += '</div>';
                contentStr += '</div>';
                contentStr += '</div>';
                contentStr += '</div>';
            });
            $('#article_list').append(contentStr);
            removeButtonDisabled(buttonName);
            if (returnData.length < 6)
            {
                $(buttonName).text('没有更多了！快去写一篇吧！');
                setButtonDisabled(buttonName);
            }
            $('#click_load_more_div').css('margin-top','40px');
        },
        complete: function () {
            var new_article_page = parseInt(article_page);
            new_article_page++;
            $('input[name="article_page"]').val(new_article_page);
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#input_article_button').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var article_title = $('input[name="article_title"]').val();
        var article_content = article_editor.txt.html();
        input_article_button('/inputArticle', token, article_title, article_content, '#input_article_button')
    }
);

function input_article_button(url, token, article_title, article_content, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            article_title:article_title,
            article_content:article_content
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    layer.msg('恭喜你！操作成功！');
                    $('input[name="article_title"]').val('');
                    article_editor.txt.html('');
                    removeButtonDisabled(buttonName);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#sign_out').on('click',function()
    {
        sign_out('/signOut')
    }
);

function sign_out(url) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{},
        beforeSend: function () {
            layer.msg('操作中...');
        },
        success:function(returnData){
            jumpUrl('/')
        },
        error:function(){
            layer.msg('网络异常..');
        }
    });
}

$('#sign_in_button').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $('input[name="account"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        sign_in_button('/signIn', token, account, password, '#sign_in_button')
    }
);

function sign_in_button(url, token, account, password, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            account:account,
            password:password
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    layer.msg('恭喜你！登录成功！');
                    jumpUrl('/mySecret');
                    break;
                case 603:
                    layer.msg('账号或密码有误！');
                    removeButtonDisabled(buttonName);
                    break;
                case 604:
                    layer.msg('该账号已被管理员封禁！');
                    removeButtonDisabled(buttonName);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#forget_password_button').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $('input[name="account"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        var email_code = $('input[name="email_code"]').val();
        forget_password_button('/forgetPassword', token, account, password, email_code, '#forget_password_button')
    }
);

function forget_password_button(url, token, account, password, email_code, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            account:account,
            password:password,
            email_code:email_code
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    layer.msg('恭喜你！修改成功！');
                    jumpUrl('/signIn');
                    break;
                case 602:
                    layer.msg('验证码有误！');
                    removeButtonDisabled(buttonName);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#sign_up_button').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $('input[name="account"]').val();
        var name = $('input[name="name"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        var email_code = $('input[name="email_code"]').val();
        sign_up('/signUp', token, account, name, password, email_code, '#sign_up_button')
    }
);

function sign_up(url, token, account, name, password, email_code, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            account:account,
            name:name,
            password:password,
            email_code:email_code
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    layer.msg('恭喜你！注册成功！');
                    removeButtonDisabled(buttonName);
                    break;
                case 601:
                    layer.msg('该邮箱账号已被注册！');
                    removeButtonDisabled(buttonName);
                    break;
                case 602:
                    layer.msg('验证码有误！');
                    removeButtonDisabled(buttonName);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

$('#send_email_code').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $('input[name="account"]').val();
        send_email_code('/sendEmailCode', token, account, '#send_email_code')
    }
);

function send_email_code(url, token, account, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            email_address:account
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('发送中');
        },
        success:function(returnData){
            switch (returnData)
            {
                case 200:
                    $(buttonName).text('已发送至您的邮箱！');
                    break;
                default:
                    layer.msg(returnData);
                    $(buttonName).text('点击发送验证码');
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..');
            removeButtonDisabled(buttonName);
        }
    });
}

function getToken() {
    $.ajax({
        url:"/getToken",
        type:"post",
        dataType:"json",
        data:{
            key:'LONG'
        },
        success:function(returnData){
            switch (returnData)
            {
                case 500:
                    layer.msg('内部错误');
                    break;
                default:
                    $('input[name="__token__"]').val(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function jumpUrl(url) {
    setTimeout(function(){
        window.location.href = url;
    }, jump_time_out);
}

function setButtonDisabled(buttonName) {
    $(buttonName).attr({ disabled: "disabled" });
}

function removeButtonDisabled(buttonName) {
    $(buttonName).removeAttr("disabled");
}